package com.youlai.boot.modules.contact.controller;

import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.contact.model.ContactForm;
import com.youlai.boot.modules.contact.model.ContactVO;
import com.youlai.boot.modules.contact.service.ContactService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Tag(name = "联系人接口")
@RestController
@RequestMapping("/api/v1/contact")
@RequiredArgsConstructor
public class ContactController {

    private final ContactService contactService;

    @Operation(summary = "联系人分页列表")
    @GetMapping("/page")
    public PageResult<ContactVO> listContacts(
            @RequestParam(required = false) Long merchantId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<ContactVO> result = contactService.getContactPage(merchantId, pageNum, pageSize);
        return PageResult.success(result);
    }

    @Operation(summary = "联系人详情")
    @GetMapping("/{id}")
    public Result<ContactVO> getContact(@PathVariable Long id) {
        ContactVO contact = contactService.getContactById(id);
        return Result.success(contact);
    }

    @Operation(summary = "添加联系人")
    @PostMapping
    public Result<Long> addContact(@Valid @RequestBody ContactForm contact) {
        Long id = contactService.saveContact(contact);
        return Result.success(id);
    }

    @Operation(summary = "修改联系人")
    @PutMapping("/{id}")
    public Result<Boolean> updateContact(
            @PathVariable Long id,
            @Valid @RequestBody ContactForm contact) {
        contact.setId(id);
        contactService.updateContact(contact);
        return Result.success(true);
    }

    @Operation(summary = "删除联系人")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteContact(@PathVariable Long id) {
        boolean result = contactService.deleteContact(id);
        return Result.judge(result);
    }
}